// pages/addManagers/addManagers.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orgId:'',
    list:[],
    orgOut:[],
    num:0,
    tatal:0,
  },
  goaddManager(){
    wx.navigateTo({
      url: '/pages/addManager/addManager?id=' + this.data.orgId
    })
  },
  getlist(){
    var that=this
    app.req({
      url: "orgmini/org/newGetOrgUserNoPage",
      data:{
        orgId: that.data.orgId
      },
      success: function (res) {
        if (200 == res.code) {
          that.setData({
            list:res.data
          })
        }
        console.log('组织人员',that.data.list)
        that.numberadd()
      }
    });
  },
  outSelection(e){
    var i = e.currentTarget.dataset.index
    var n = this.data.num
    var arr = this.data.orgOut
    if (arr[i].isAdmin == 0) {
      arr[i].isAdmin = 1
      n++
    } else {
      arr[i].isAdmin = 0
      n--
    }
    this.setData({
      orgOut: arr,
      num:n
    })
  },
  orgselection(e){
    var i = e.currentTarget.dataset.index
    var arr = this.data.list
    console.log('arr',arr,i)
    var m = this.data.num
    if (arr[i].isAdmin==0) {
      arr[i].isAdmin = 1
      m++
    } else {
      arr[i].isAdmin = 0
      m--
    }
    this.setData({
      list: arr,
      num:m
    })
  },
  numberadd(){
    console.log('jjjjjj', j)
    var i =0;
    var j = 0;
    if (this.data.orgOut.length>0){
      for(var a in this.data.orgOut){
        if (this.data.orgOut[a].isAdmin==1){
          i++
        }
      }
    } 
    if(this.data.list.length>0){
      for (var b in this.data.list) {
        if (this.data.list[b].isAdmin == 1) {
          j++
        }
      }
      console.log('jjjjjjd',j)
    }
    this.setData({
      num:i+j,
      tatal: this.data.orgOut.length + this.data.list.length
    })
  },
  save(){
    var that=this
    var lists= []
    var orgOuts =[]
    for(var i in that.data.list){
      if (that.data.list[i].isAdmin==1){
        lists.push(that.data.list[i].id)
      }
    }
    for (var j in that.data.orgOut) {
      if (that.data.orgOut[j].isAdmin == 1) {
        orgOuts.push(that.data.orgOut[j])
      }
    }
    app.req({
      url: "orgmini/org/newAddAdmin",
      data: {
        orgId: that.data.orgId,
        onOrgList: lists,
        orgOut: orgOuts,
      },
      success: function (res) {
        if (200 == res.code) {
          wx.showToast({
            title: '添加成功',
          })
          wx.navigateBack({
            delta: 1
          })
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      orgId: options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    that.getlist()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})